package com.wgl.utl;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.LinkedHashMap;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.apache.http.client.ClientProtocolException;

import com.wgl.db.pojo.Train;

public class RemainTicketThread implements Runnable {

	private String fromStation = null;
	private String toStation = null;
	private String trainCode = null;
	private String trainDate = null;
	private String passengerType = null;

	public RemainTicketThread(String fromStation, String toStation, String trainDate, String trainCode,
			String passengerType) throws ClientProtocolException, URISyntaxException, IOException, ParseException,
			AddressException, MessagingException {
		this.fromStation = fromStation;
		this.toStation = toStation;
		this.trainDate = trainDate;
		this.trainCode = trainCode;
		this.passengerType = passengerType;

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		LinkedHashMap<String, String> condition = new LinkedHashMap<String, String>();
		condition.put("leftTicketDTO.train_date", trainDate);
		condition.put("leftTicketDTO.from_station", fromStation);
		condition.put("leftTicketDTO.to_station", toStation);
		condition.put("purpose_codes", Tool.StringUtil.validStr(this.passengerType) ? this.passengerType : "ADULT");
		Train train = null;
		// while (true) {
		try {
			Thread.sleep(5000);
			train = Train.getTargetTrainListByCondition(condition, trainCode, 1);
			if (train != null) {
				// 发短信
				StringBuffer sb = new StringBuffer();
				sb.append("您好," + "你所订阅的车次");
				sb.append(train.getTrainCode());
				sb.append("(" + train.getTrainDate() + "，" + train.getStartTime() + ")有票啦!赶紧买吧");
				try {
					Tool.MailUtil.sendSimpleEMail163("wglbsr@163.com", "paladin321", "有票啦", sb.toString(),
							"wglbsr@outlook.com");
					System.out.println("完成!");
				} catch (AddressException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				break;
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// }
	}

	public static void main(String[] args) throws AddressException, ClientProtocolException, URISyntaxException,
			IOException, ParseException, MessagingException {
		// TODO Auto-generated method stub
		Thread t = new Thread(new RemainTicketThread("QBQ", "IZQ", "2018-06-26", "G6101", "ADULT"));
		t.start();
	}

}
